/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedbean.RawMaterialPurchase;

import java.util.Collection;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import merlion.frm.bpm.entity.SupplierInvoice;
import merlion.frm.bpm.session.ProcurementBPSessionBeanLocal;
import merlion.scm.icm.session.InventoryControlSessionBeanLocal;
import merlion.scm.rmpm.entity.CriterionRating;
import merlion.scm.rmpm.entity.GoodsReceipt;
import merlion.scm.rmpm.entity.GoodsReceiptRating;
import merlion.scm.rmpm.entity.Supplier;
import merlion.scm.rmpm.session.RawMaterialPurchaseSessionBeanLocal;

/**
 *
 * @author Kenn
 */
@ManagedBean(name = "rsManagerBean")
@ViewScoped
public class RateSupplierManagerBean {

    @EJB
    private RawMaterialPurchaseSessionBeanLocal rmpSessionBean;
    @EJB
    private ProcurementBPSessionBeanLocal pbpSessionBean;
    @EJB
    private InventoryControlSessionBeanLocal icSessionBean;
    private Supplier supplier;
    private GoodsReceipt goodsReceipt;
    private String statusMsg;

    //--------------------------------------------------------------------------
    //                          Constructor
    //--------------------------------------------------------------------------
    public RateSupplierManagerBean() {

        supplier = (Supplier) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("supplier");
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().remove("supplier");

        goodsReceipt = (GoodsReceipt) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("goodsReceipt");
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().remove("goodsReceipt");

        statusMsg = (String) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("statusMsg");
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().remove("statusMsg");

        if (supplier == null) {
            supplier = new Supplier();
        }
        if (goodsReceipt == null) {
            goodsReceipt = new GoodsReceipt();
        }
    }

    //--------------------------------------------------------------------------
    //                          Manager Bean Methods
    //--------------------------------------------------------------------------
    public Collection<Supplier> getAllSupplier() {
        return rmpSessionBean.getAllSupplier();
    }

    public Collection<GoodsReceipt> getUnratedGoodsReceipt() {
        return rmpSessionBean.getUnratedGoodsReceiptBySupplier(supplier);
    }

    public Collection<GoodsReceipt> getRatedGoodsReceipt() {
        return rmpSessionBean.getRatedGoodsReceiptBySupplier(supplier);
    }

    public String rateGoodsReceipt(Long id) {

        goodsReceipt = icSessionBean.getGoodsReceiptById(id);

        GoodsReceiptRating rating = new GoodsReceiptRating();
        goodsReceipt.setRating(rating);
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("goodsReceipt", goodsReceipt);

        return "rateSupplierGoodsReceipt?faces-redirect=true";
    }

    public String rate() {

        GoodsReceiptRating i = goodsReceipt.getRating();

        //Validate
        boolean valid = true;
        for (CriterionRating c : i.getRatings()) {
            if (c.getRating() == null) {
                valid = false;
            }
        }
        if(!valid){
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "All ratings are required", ""));
            return null;
        }
        for (CriterionRating c : i.getRatings()) {
            if (c.getRating() <= 0 || c.getRating() >= 6 ) {
                valid = false;
            }
        }
        if(!valid){
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Rating must be an integer from 1 - 5", ""));
            return null;
        }

        i.calculateOverallRating();
        goodsReceipt = icSessionBean.getGoodsReceiptById(goodsReceipt.getId());
        goodsReceipt.setRating(i);
        goodsReceipt = icSessionBean.updateGoodsReceipt(goodsReceipt);

        if (goodsReceipt != null) {
            rmpSessionBean.calculateSupplierRating(goodsReceipt.getSupplier());
            FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("statusMsg", "Invoice rating successfully saved.");
        } else {
            FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("statusMsg", "Invoice rating failed to save. Please try again.");
        }

        return "rateSupplierGoodsReceiptStatus?faces-redirect=true";
    }

    public String rateGoodsReceiptDisplay(Long id) {

        goodsReceipt = icSessionBean.getGoodsReceiptById(id);

        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("goodsReceipt", goodsReceipt);
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("supplier", supplier);

        return "rateSupplierGoodsReceiptDisplay?faces-redirect=true";
    }

    public String back() {

        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("supplier", supplier);
        return "rateSupplier?faces-redirect=true";

    }
    //--------------------------------------------------------------------------
    //                          Getter and Setter
    //--------------------------------------------------------------------------

    public Supplier getSupplier() {
        return supplier;
    }

    public void setSupplier(Supplier supplier) {
        this.supplier = supplier;
    }

    public GoodsReceipt getGoodsReceipt() {
        return goodsReceipt;
    }

    public void setGoodsReceipt(GoodsReceipt goodsReceipt) {
        this.goodsReceipt = goodsReceipt;
    }

    public String getStatusMsg() {
        return statusMsg;
    }

    public void setStatusMsg(String statusMsg) {
        this.statusMsg = statusMsg;
    }
}
